﻿using System.Web.UI;

namespace RealDolmen.Controls
{
    public class ControlHelper
    {
        public static T GetClosestParent<T>(Control control) where T : class
        {
            if (control is T)
                return control as T;
            else if (control.Parent == null)
                return null;
            else
                return GetClosestParent<T>(control.Parent);
        }
        public static Control GetControlDown<T>(Control parent, bool recursive) where T : Control
        {
            Control ctrl = null;

            for (int i = 0; i < parent.Controls.Count; i++)
            {
                Control temp = parent.Controls[i];

                if (temp is T)
                    ctrl = temp;
            }

            if (ctrl == null && recursive)
            {
                for (int i = 0; i < parent.Controls.Count && ctrl == null; i++)
                {
                    ctrl = GetControlDown<T>(parent.Controls[i], recursive);
                }
            }
            return ctrl;
        }
    }
}
